//
//  QRCScannerViewController.m
//  QRScannerDemo
//  blog:www.zhangfei.tk
//  Created by zhangfei on 15/10/15.
//  Copyright © 2015年 zhangfei. All rights reserved.
//

#import "QRScannerViewController.h"
#import "QRCScanner.h"

@interface QRScannerViewController ()<QRCodeScanneDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic, copy) NSString *result;

@end

@implementation QRScannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

#pragma mark - 从相册获取二维码图片
- (void)readerImage{
    UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
    photoPicker.delegate = self;
    photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    photoPicker.view.backgroundColor = [UIColor whiteColor];
    [self presentViewController:photoPicker animated:YES completion:NULL];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage *srcImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSString *result = [QRCScanner scQRReaderForImage:srcImage];
    self.result = result;
    NSLog(@"扫描的结果是: %@", self.result);
    if (self.callBackBlock) {
        self.callBackBlock(result);
    }

    [self dismissViewController];
}

- (void) initView {
    QRCScanner *scanner = [[QRCScanner alloc]initQRCScannerWithView:self.view];
    scanner.delegate = self;
    [self.view addSubview:scanner];
    
    //从相册选取二维码
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"相册" style:UIBarButtonItemStylePlain target:self action:@selector(readerImage)];
    // 取消
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(dismissViewController)];
}

- (void)dismissViewController {
    for (UIViewController * VC in self.navigationController.viewControllers) {
        [VC dismissViewControllerAnimated:YES completion:nil];
    }
}


#pragma mark - 扫描二维码成功后结果的代理方法
- (void)didFinshedScanningQRCode:(NSString *)result{
    NSLog(@"%@", result);
    NSData *data = [result dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString *base64String = [data base64EncodedStringWithOptions:0];
    NSDictionary *dict = @{
                           @"code": @0,
                           @"codeStr": base64String
                           };
    NSString *resultStr =  [self toJSONData:dict];
    if (self.callBackBlock) {
        self.callBackBlock(resultStr);
    }
    [self dismissViewController];
}

- (NSString*)toJSONData:(id)theData{
    NSString* jsonString = @"";
    if(theData != nil) {
        NSData*jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                          options:NSJSONWritingPrettyPrinted
                                                            error:nil];
        if([jsonData length] != 0){
            jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        }
    }
    return jsonString;
}

@end

